home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEVWPRN.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  19KB  |  646 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevwprn.c */
  20. /*
  21.  * Microsoft Windows 3.n printer driver for Ghostscript.
  22.  * Original version by Russell Lang and
  23.  * L. Peter Deutsch, Aladdin Enterprises.
  24.  */
  25. #include "gdevmswn.h"
  26. #include "gp.h"
  27. #include "commdlg.h"
  28.  
  29. /*
  30.  ****** NOTE: this module and gdevwddb should be refactored.
  31.  * The drawing routines are almost identical.
  32.  * The differences are that the mswinprn doesn't use an extra
  33.  * palette (gdevwddb.c could probably be made to work with 
  34.  * one palette also), mswinprn doesn't call win_update() because
  35.  * hwndimg doesn't exist, and the HDC is hdcmf not hdcbit.
  36.  ******/
  37.  
  38. /* Make sure we cast to the correct structure type. */
  39. typedef struct gx_device_win_prn_s gx_device_win_prn;
  40. #undef wdev
  41. #define wdev ((gx_device_win_prn *)dev)
  42.  
  43. /* Forward references */
  44. private void near win_prn_addtool(P2(gx_device_win_prn *, int));
  45. private void near win_prn_maketools(P2(gx_device_win_prn *, HDC));
  46. private void near win_prn_destroytools(P1(gx_device_win_prn *));
  47.  
  48. /* Device procedures */
  49.  
  50. /* See gxdevice.h for the definitions of the procedures. */
  51. private dev_proc_open_device(win_prn_open);
  52. private dev_proc_close_device(win_prn_close);
  53. private dev_proc_sync_output(win_prn_sync_output);
  54. private dev_proc_output_page(win_prn_output_page);
  55. private dev_proc_map_rgb_color(win_prn_map_rgb_color);
  56. private dev_proc_fill_rectangle(win_prn_fill_rectangle);
  57. private dev_proc_tile_rectangle(win_prn_tile_rectangle);
  58. private dev_proc_copy_mono(win_prn_copy_mono);
  59. private dev_proc_copy_color(win_prn_copy_color);
  60. private dev_proc_draw_line(win_prn_draw_line);
  61.  
  62. /* The device descriptor */
  63. struct gx_device_win_prn_s {
  64.     gx_device_common;
  65.     gx_device_win_common;
  66.  
  67.     /* Handles */
  68.  
  69.     HPEN hpen, *hpens;
  70.     uint hpensize;
  71.     HBRUSH hbrush, *hbrushs;
  72.     uint hbrushsize;
  73. #define select_brush(color)\
  74.   if (wdev->hbrush != wdev->hbrushs[color])\
  75.    {    wdev->hbrush = wdev->hbrushs[color];\
  76.     SelectObject(wdev->hdcmf,wdev->hbrush);\
  77.    }
  78.     /* A staging bitmap for copy_mono. */
  79.     /* We want one big enough to handle the standard 16x16 halftone; */
  80.     /* this is also big enough for ordinary-size characters. */
  81.  
  82. #define bmWidthBytes 4        /* must be even */
  83. #define bmWidthBits (bmWidthBytes * 8)
  84. #define bmHeight 32
  85.     HBITMAP FAR hbmmono;
  86.     HDC FAR hdcmono;
  87.     gx_bitmap_id bm_id;
  88.  
  89.     HDC hdcprn;
  90.     HDC hdcmf;
  91.     char mfname[128];
  92.     DLGPROC lpfnAbortProc;
  93. };
  94. private gx_device_procs win_prn_procs = {
  95.     win_prn_open,
  96.     gx_default_get_initial_matrix,
  97.     win_prn_sync_output,
  98.     win_prn_output_page,
  99.     win_prn_close,
  100.     win_prn_map_rgb_color,
  101.     win_map_color_rgb,
  102.     win_prn_fill_rectangle,
  103.     win_prn_tile_rectangle,
  104.     win_prn_copy_mono,
  105.     win_prn_copy_color,
  106.     win_prn_draw_line,
  107.     gx_default_get_bits,
  108.     gx_default_get_props,
  109.     gx_default_put_props,
  110.     gx_default_map_cmyk_color,
  111.     win_get_xfont_procs
  112. };
  113. gx_device_win_prn gs_mswinprn_device = {
  114.     sizeof(gx_device_win_prn),
  115.     &win_prn_procs,
  116.     "mswinprn",
  117.     INITIAL_WIDTH, INITIAL_HEIGHT,     /* win_prn_open() fills these in later */
  118.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,    /* win_prn_open() fills these in later */
  119.     no_margins,
  120.     dci_black_and_white,
  121.     0,        /* not open yet */
  122.     2,        /* nColors */
  123. };
  124.  
  125. /* Open the win_prn driver */
  126. private int
  127. win_prn_open(gx_device *dev)
  128. {    int depth;
  129.     PRINTDLG pd;
  130.     FILE *f;
  131.     POINT offset;
  132.     POINT size;
  133.  
  134.     _fmemset(&pd, 0, sizeof(PRINTDLG));
  135.     pd.lStructSize = sizeof(PRINTDLG);
  136.     pd.hwndOwner = hwndtext;
  137.     pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  138.     if (!PrintDlg(&pd)) {
  139.         /* device not opened - exit ghostscript */
  140.         return gs_error_limitcheck;
  141.     }
  142.     GlobalFree(pd.hDevMode);
  143.     GlobalFree(pd.hDevNames);
  144.     pd.hDevMode = pd.hDevNames = NULL;
  145.     wdev->hdcprn = pd.hDC;
  146.     if (!(GetDeviceCaps(wdev->hdcprn, RASTERCAPS) != RC_BITBLT)) {
  147.         DeleteDC(wdev->hdcprn);
  148.         return gs_error_limitcheck;
  149.     }
  150.  
  151.     wdev->lpfnAbortProc = (DLGPROC)MakeProcInstance((FARPROC)AbortProc,phInstance);
  152.     Escape(wdev->hdcprn,SETABORTPROC,0,(LPSTR)wdev->lpfnAbortProc,NULL);  
  153.     if (Escape(wdev->hdcprn, STARTDOC, strlen(szAppName),szAppName, NULL) <= 0) {
  154.         FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  155.         DeleteDC(wdev->hdcprn);
  156.         return gs_error_limitcheck;
  157.     }
  158.  
  159.     f = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  160.             wdev->mfname, "wb");
  161.     if (f == (FILE *)NULL) {
  162.         Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  163.         FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  164.         DeleteDC(wdev->hdcprn);
  165.         return  gs_error_limitcheck;
  166.     }
  167.     unlink(wdev->mfname);
  168.     wdev->hdcmf = CreateMetaFile(wdev->mfname);
  169.  
  170.     dev->x_pixels_per_inch = GetDeviceCaps(wdev->hdcprn, LOGPIXELSX);
  171.     dev->y_pixels_per_inch = GetDeviceCaps(wdev->hdcprn, LOGPIXELSY);
  172.     Escape(wdev->hdcprn, GETPHYSPAGESIZE, NULL, NULL, (LPPOINT)&size);
  173.     dev->width = size.x;
  174.     dev->height = size.y;
  175.     Escape(wdev->hdcprn, GETPRINTINGOFFSET, NULL, NULL, (LPPOINT)&offset);
  176.     dev->l_margin = offset.x / dev->x_pixels_per_inch;
  177.     dev->t_margin = offset.y / dev->y_pixels_per_inch;
  178.     dev->r_margin =
  179.         (size.x - offset.x - GetDeviceCaps(wdev->hdcprn, HORZRES))
  180.          / dev->x_pixels_per_inch;
  181.     dev->b_margin =
  182.         (size.y - offset.y - GetDeviceCaps(wdev->hdcprn, VERTRES))
  183.          / dev->y_pixels_per_inch
  184.         + 0.15;  /* hack to add a bit more margin for deskjet printer */
  185.  
  186.     wdev->hdctext = NULL;
  187.  
  188.     /* Set parameters that were unknown before opening device */
  189.     /* Find out if the device supports color */
  190.     /* We recognize 2, 16 or 256 color devices */
  191.     depth = GetDeviceCaps(wdev->hdcprn,PLANES) * GetDeviceCaps(wdev->hdcprn,BITSPIXEL);
  192.     if ( depth >= 8 ) { /* use 64 static colors and 166 dynamic colors from 8 planes */
  193.         static const gx_device_color_info win_256color = dci_color(8,31,4);
  194.         dev->color_info = win_256color;
  195.         wdev->nColors = 64;
  196.     }
  197.     else if ( depth >= 4 ) {
  198.         static const gx_device_color_info win_16ega_color = dci_color(4, 2, 3);
  199.         dev->color_info = win_16ega_color;
  200.         wdev->nColors = 16;
  201.     } 
  202.     else {   /* default is black_and_white */
  203.         wdev->nColors = 2;
  204.     }
  205.  
  206.     /* create palette for display */
  207.     if ((wdev->limgpalette = win_makepalette((gx_device_win *)dev))
  208.         == (LPLOGPALETTE)NULL) {
  209.         HMETAFILE hmf = CloseMetaFile(wdev->hdcmf);
  210.         DeleteMetaFile(hmf);
  211.         unlink(wdev->mfname);
  212.         Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  213.             FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  214.             DeleteDC(wdev->hdcprn);
  215.         return win_nomemory();
  216.     }
  217.     wdev->himgpalette = CreatePalette(wdev->limgpalette);
  218.  
  219.     /* Create the bitmap and DC for copy_mono. */
  220.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  221.     wdev->hdcmono = CreateCompatibleDC(wdev->hdcprn);
  222.     if ( wdev->hbmmono == NULL || wdev->hdcmono == NULL ) {
  223.         HMETAFILE hmf = CloseMetaFile(wdev->hdcmf);
  224.         DeleteMetaFile(hmf);
  225.         unlink(wdev->mfname);
  226.         Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  227.             FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  228.             DeleteDC(wdev->hdcprn);
  229.         gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) + 
  230.             (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  231.             "win_prn_open");
  232.         return win_nomemory();
  233.     }
  234.     SetMapMode(wdev->hdcmono, GetMapMode(wdev->hdcprn));
  235.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  236.     (void) SelectPalette(wdev->hdcmf,wdev->himgpalette,NULL);
  237.     RealizePalette(wdev->hdcmf);
  238.     win_prn_maketools(wdev,wdev->hdcmf);
  239.     wdev->bm_id = gx_no_bitmap_id;
  240.  
  241.     return 0;
  242. }
  243.  
  244.  
  245. /* Close the win_prn driver */
  246. private int
  247. win_prn_close(gx_device *dev)
  248. {
  249. HMETAFILE hmf;
  250.     /* Free resources */
  251.     Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  252.     FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  253.     DeleteDC(wdev->hdcprn);
  254.     hmf = CloseMetaFile(wdev->hdcmf);
  255.     DeleteMetaFile(hmf);
  256.     unlink(wdev->mfname);
  257.  
  258.     win_prn_destroytools(wdev);
  259.     DeleteDC(wdev->hdcmono);
  260.     DeleteObject(wdev->hbmmono);
  261.     DeleteObject(wdev->himgpalette);
  262.     gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) + 
  263.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  264.         "win_prn_close");
  265.     return(0);
  266. }
  267.  
  268. /* Do nothing */
  269. int
  270. win_prn_sync_output(gx_device *dev)
  271. {
  272.     return 0;
  273. }
  274.  
  275. /* Write page to printer */
  276. int
  277. win_prn_output_page(gx_device *dev, int copies, int flush)
  278. {
  279. RECT rect;
  280. HMETAFILE hmf;
  281.     hmf = CloseMetaFile(wdev->hdcmf);
  282.     
  283.     Escape(wdev->hdcprn, NEXTBAND, NULL, NULL, (LPRECT)&rect);
  284.     while (!IsRectEmpty(&rect)) {
  285.         PlayMetaFile(wdev->hdcprn, hmf);
  286.         if (Escape(wdev->hdcprn, NEXTBAND, NULL, NULL, (LPRECT)&rect) <= 0)
  287.         break;
  288.     }
  289.     DeleteMetaFile(hmf);
  290.     unlink(wdev->mfname);
  291.     wdev->hdcmf = CreateMetaFile(wdev->mfname);
  292.     (void) SelectPalette(wdev->hdcmf,wdev->himgpalette,NULL);
  293.     RealizePalette(wdev->hdcmf);
  294.     SelectObject(wdev->hdcmf,wdev->hpen);
  295.     SelectObject(wdev->hdcmf,wdev->hbrush);
  296.  
  297.     return 0;
  298. }
  299.  
  300.  
  301. /* Map a r-g-b color to the colors available under Windows */
  302. private gx_color_index
  303. win_prn_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  304.   gx_color_value b)
  305. {    int i = wdev->nColors;
  306.     gx_color_index color = win_map_rgb_color(dev, r, g, b);
  307.     if ( color != i ) return color;
  308.     (void) SelectPalette(wdev->hdcmf,wdev->himgpalette,NULL);
  309.     RealizePalette(wdev->hdcmf);
  310.     win_prn_addtool(wdev, i);
  311.  
  312.     return color;
  313. }
  314.  
  315.  
  316. /* Macro for filling a rectangle with a color. */
  317. /* Note that it starts with a declaration. */
  318. #define fill_rect(x, y, w, h, color)\
  319. RECT rect;\
  320. rect.left = x, rect.top = y;\
  321. rect.right = x + w, rect.bottom = y + h;\
  322. FillRect(wdev->hdcmf, &rect, wdev->hbrushs[(int)color])
  323.  
  324.  
  325. /* Fill a rectangle. */
  326. private int
  327. win_prn_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  328.   gx_color_index color)
  329. {
  330.     fit_fill(dev, x, y, w, h);
  331.     /* Use PatBlt for filling.  Special-case black. */
  332.     if ( color == 0 )
  333.         PatBlt(wdev->hdcmf, x, y, w, h, rop_write_0s);
  334.     else
  335.     {    select_brush((int)color);
  336.         PatBlt(wdev->hdcmf, x, y, w, h, rop_write_pattern);
  337.     }
  338.  
  339.     return 0;
  340. }
  341.  
  342. /* Tile a rectangle.  If neither color is transparent, */
  343. /* pre-clear the rectangle to color0 and just tile with color1. */
  344. /* This is faster because of how win_copy_mono is implemented. */
  345. /* Note that this also does the right thing for colored tiles. */
  346. private int
  347. win_prn_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  348.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  349.   int px, int py)
  350. {    fit_fill(dev, x, y, w, h);
  351.     if ( czero != gx_no_color_index && cone != gx_no_color_index )
  352.        {    fill_rect(x, y, w, h, czero);
  353.         czero = gx_no_color_index;
  354.        }
  355.     if ( tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  356.          (px | py) == 0 && cone != gx_no_color_index
  357.        )
  358.     {    /* We can do this much more efficiently */
  359.         /* by using the internal algorithms of copy_mono */
  360.         /* and gx_default_tile_rectangle. */
  361.         int width = tile->size.x;
  362.         int height = tile->size.y;
  363.         int rwidth = tile->rep_width;
  364.         int irx = ((rwidth & (rwidth - 1)) == 0 ? /* power of 2 */
  365.             x & (rwidth - 1) :
  366.             x % rwidth);
  367.         int ry = y % tile->rep_height;
  368.         int icw = width - irx;
  369.         int ch = height - ry;
  370.         int ex = x + w, ey = y + h;
  371.         int fex = ex - width, fey = ey - height;
  372.         int cx, cy;
  373.  
  374.         select_brush((int)cone);
  375.  
  376.         if ( tile->id != wdev->bm_id || tile->id == gx_no_bitmap_id )
  377.         {    wdev->bm_id = tile->id;
  378.             SetBitmapBits(wdev->hbmmono,
  379.                       (DWORD)(bmWidthBytes * tile->size.y),
  380.                       (BYTE *)tile->data);
  381.         }
  382.  
  383. #define copy_tile(srcx, srcy, tx, ty, tw, th)\
  384.   BitBlt(wdev->hdcmf, tx, ty, tw, th, wdev->hdcmono, srcx, srcy, rop_write_at_1s)
  385.  
  386.         if ( ch > h ) ch = h;
  387.         for ( cy = y; ; )
  388.            {    if ( w <= icw )
  389.                 copy_tile(irx, ry, x, cy, w, ch);
  390.             else
  391.             {    copy_tile(irx, ry, x, cy, icw, ch);
  392.                 cx = x + icw;
  393.                 while ( cx <= fex )
  394.                 {    copy_tile(0, ry, cx, cy, width, ch);
  395.                     cx += width;
  396.                 }
  397.                 if ( cx < ex )
  398.                 {    copy_tile(0, ry, cx, cy, ex - cx, ch);
  399.                 }
  400.             }
  401.             if ( (cy += ch) >= ey ) break;
  402.             ch = (cy > fey ? ey - cy : height);
  403.             ry = 0;
  404.            }
  405.  
  406.         return 0;
  407.     }
  408.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  409. }
  410.  
  411.  
  412. /* Draw a line */
  413. private int
  414. win_prn_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  415.   gx_color_index color)
  416. {
  417.     if (wdev->hpen != wdev->hpens[(int)color]) {
  418.         wdev->hpen = wdev->hpens[(int)color];
  419.         SelectObject(wdev->hdcmf,wdev->hpen);
  420.     }
  421.     MoveTo(wdev->hdcmf, x0, y0);
  422.     LineTo(wdev->hdcmf, x1, y1);
  423.     return 0;
  424. }
  425.  
  426. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  427. /* Color = gx_no_color_index means transparent (no effect on the image). */
  428. private int
  429. win_prn_copy_mono(gx_device *dev,
  430.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  431.   int x, int y, int w, int h,
  432.   gx_color_index zero, gx_color_index one)
  433. {    int endx;
  434.     const byte *ptr_line;
  435.     int width_bytes, height;
  436.     DWORD rop = rop_write_at_1s;
  437.     int color;
  438.     BYTE aBit[bmWidthBytes * bmHeight];
  439.     BYTE *aptr = aBit;
  440.  
  441.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  442.  
  443.     if ( sourcex & ~7 )
  444.     {    base += sourcex >> 3;
  445.         sourcex &= 7;
  446.     }
  447.  
  448.     /* Break up large transfers into smaller ones. */
  449.     while ( (endx = sourcex + w) > bmWidthBits )
  450.     {    int lastx = (endx - 1) & -bmWidthBits;
  451.         int subw = endx - lastx;
  452.         int code = win_prn_copy_mono(dev, base, lastx,
  453.                          raster, gx_no_bitmap_id,
  454.                          x + lastx - sourcex, y,
  455.                          subw, h, zero, one);
  456.         if ( code < 0 ) return code;
  457.         w -= subw;
  458.     }
  459.     while ( h > bmHeight )
  460.     {    int code;
  461.         h -= bmHeight;
  462.         code = win_prn_copy_mono(dev, base + h * raster, sourcex,
  463.                      raster, gx_no_bitmap_id,
  464.                      x, y + h, w, bmHeight, zero, one);
  465.         if ( code < 0 ) return code;
  466.     }
  467.  
  468.     width_bytes = (sourcex + w + 7) >> 3;
  469.     ptr_line = base;
  470.  
  471.     if ( zero == gx_no_color_index )
  472.        {    if ( one == gx_no_color_index ) return 0;
  473.         color = (int)one;
  474.         if ( color == 0 )
  475.             rop = rop_write_0_at_1s;
  476.         else
  477.             select_brush(color);
  478.        }
  479.     else
  480.        {    if ( one == gx_no_color_index )
  481.            {    color = (int)zero;
  482.             rop = rop_write_at_0s;
  483.            }
  484.         else
  485.            {    /* Pre-clear the rectangle to zero */
  486.             fill_rect(x, y, w, h, zero);
  487.             color = (int)one;
  488.            }
  489.         select_brush(color);
  490.        }
  491.  
  492.     if ( id != wdev->bm_id || id == gx_no_bitmap_id )
  493.     {    wdev->bm_id = id;
  494.         if ( raster == bmWidthBytes )
  495.         {    /* We can do the whole thing in a single transfer! */
  496.             SetBitmapBits(wdev->hbmmono,
  497.                       (DWORD)(bmWidthBytes * h),
  498.                       (BYTE *)base);
  499.         }
  500.         else
  501.         {    for ( height = h; height--;
  502.                   ptr_line += raster, aptr += bmWidthBytes
  503.                 )
  504.             {    /* Pack the bits into the bitmap. */
  505.                 switch ( width_bytes )
  506.                 {
  507.                     default: memcpy(aptr, ptr_line, width_bytes); break;
  508.                     case 4: aptr[3] = ptr_line[3];
  509.                     case 3: aptr[2] = ptr_line[2];
  510.                     case 2: aptr[1] = ptr_line[1];
  511.                     case 1: aptr[0] = ptr_line[0];
  512.                 }
  513.             }
  514.             SetBitmapBits(wdev->hbmmono,
  515.                       (DWORD)(bmWidthBytes * h),
  516.                       &aBit[0]);
  517.         }
  518.     }
  519.  
  520.     BitBlt(wdev->hdcmf, x, y, w, h, wdev->hdcmono, sourcex, 0, rop);
  521.     return 0;
  522. }
  523.  
  524.  
  525. /* Copy a color pixel map.  This is just like a bitmap, except that */
  526. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  527. private int
  528. win_prn_copy_color(gx_device *dev,
  529.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  530.   int x, int y, int w, int h)
  531. {
  532.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  533.  
  534.     if ( gx_device_has_color(dev) )
  535.     {
  536.     switch(dev->color_info.depth) {
  537.       case 8:
  538.         {    int xi, yi;
  539.         int skip = raster - w;
  540.         const byte *sptr = base + sourcex;
  541.           if ( w <= 0 ) return 0;
  542.           if ( x < 0 || x + w > dev->width )
  543.             return_error(gs_error_rangecheck);
  544.         for ( yi = y; yi - y < h; yi++ )
  545.            {
  546.             for ( xi = x; xi - x < w; xi++ )
  547.                {    int color =  *sptr++;
  548.                 SetPixel(wdev->hdcmf,xi,yi,PALETTEINDEX(color));
  549.                }
  550.             sptr += skip;
  551.            }
  552.         }
  553.         break;
  554.       case 4:
  555.        {    /* color device, four bits per pixel */
  556.         const byte *line = base + (sourcex >> 1);
  557.         int dest_y = y, end_x = x + w;
  558.  
  559.         if ( w <= 0 ) return 0;
  560.         while ( h-- )              /* for each line */
  561.            {    const byte *source = line;
  562.             register int dest_x = x;
  563.             if ( sourcex & 1 )    /* odd nibble first */
  564.                {    int color =  *source++ & 0xf;
  565.                 SetPixel(wdev->hdcmf,dest_x,dest_y,PALETTEINDEX(color));
  566.                 dest_x++;
  567.                }
  568.             /* Now do full bytes */
  569.             while ( dest_x < end_x )
  570.                {    int color = *source >> 4;
  571.                 SetPixel(wdev->hdcmf,dest_x,dest_y,PALETTEINDEX(color));
  572.                 dest_x++;
  573.                 if ( dest_x < end_x )
  574.                    {    color =  *source++ & 0xf;
  575.                     SetPixel(wdev->hdcmf,dest_x,dest_y,PALETTEINDEX(color));
  576.                     dest_x++;
  577.                    }
  578.                }
  579.             dest_y++;
  580.             line += raster;
  581.            }
  582.        }
  583.        break;
  584.     default:
  585.         return(-1); /* panic */
  586.     }
  587.     }
  588.     else 
  589.     /* monochrome device: one bit per pixel */
  590.        {    /* bitmap is the same as win_copy_mono: one bit per pixel */
  591.         win_prn_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  592.             (gx_color_index)0, 
  593.             (gx_color_index)(dev->color_info.depth==8 ? 63 : dev->color_info.max_gray));
  594.        }
  595.     return 0;
  596. }
  597.  
  598.  
  599. /* ------ Internal routines ------ */
  600.  
  601. #undef wdev
  602.  
  603.  
  604. private void near
  605. win_prn_addtool(gx_device_win_prn *wdev, int i)
  606. {
  607.     wdev->hpens[i] = CreatePen(PS_SOLID, 1, PALETTEINDEX(i));
  608.     wdev->hbrushs[i] = CreateSolidBrush(PALETTEINDEX(i));
  609. }
  610.  
  611.  
  612. private void near
  613. win_prn_maketools(gx_device_win_prn *wdev, HDC hdc)
  614. {    int i;
  615.     wdev->hpensize = (1<<(wdev->color_info.depth)) * sizeof(HPEN);
  616.     wdev->hpens = (HPEN *)gs_malloc(1, wdev->hpensize,
  617.                     "win_prn_maketools(pens)");
  618.     wdev->hbrushsize = (1<<(wdev->color_info.depth)) * sizeof(HBRUSH);
  619.     wdev->hbrushs = (HBRUSH *)gs_malloc(1, wdev->hbrushsize,
  620.                         "win_prn_maketools(brushes)");
  621.     if (wdev->hpens && wdev->hbrushs) {
  622.         for (i=0; i<wdev->nColors; i++)
  623.             win_prn_addtool(wdev, i);
  624.  
  625.         wdev->hpen = wdev->hpens[0];
  626.         SelectObject(hdc,wdev->hpen);
  627.  
  628.         wdev->hbrush = wdev->hbrushs[0];
  629.         SelectObject(hdc,wdev->hbrush);
  630.     }
  631. }
  632.  
  633.  
  634. private void near
  635. win_prn_destroytools(gx_device_win_prn *wdev)
  636. {    int i;
  637.     for (i=0; i<wdev->nColors; i++) {
  638.         DeleteObject(wdev->hpens[i]);
  639.         DeleteObject(wdev->hbrushs[i]);
  640.     }
  641.     gs_free((char *)wdev->hbrushs, 1, wdev->hbrushsize,
  642.         "win_prn_destroytools(brushes)");
  643.     gs_free((char *)wdev->hpens, 1, wdev->hpensize,
  644.         "win_prn_destroytools(pens)");
  645. }
  646.